package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.basic.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//controller+responsbody
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    public IEmployeeService employeeService;

    /**
     * 添加对象
     *
     * @param employee 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody Employee employee) {//@RequestBody可以接收对象
        try {
            employeeService.save(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "添加失败" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败" + e.getMessage());
        }
    }

    @PatchMapping("batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Long> id) {
        try {
            employeeService.deleteAll(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败" + e.getMessage());
        }
    }

    /**
     * 修改对象
     *
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult update(@RequestBody Employee employee) {
        try {
            employeeService.update(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "修改失败" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("{id}")
    public Employee findById(@PathVariable("id") Long id) {
        return employeeService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @PatchMapping
    public PageBase<Employee> findAll(@RequestBody EmployeeQuery query) {

        return employeeService.findAll(query);
    }

    /**
     * 查询所有部门
     * @return
     */
    @PatchMapping("/loadAll")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }
}
